import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import '../assets/css/global.css'
import '../assets/font/iconfont.css'
//引入Home组件
import Home from '../components/Home.vue'
//引入welcome组件
import Welcome from '../components/Welcome.vue'
//引入用户管理组件
import User from '../components/user/User.vue'
//引入权限管理组件
import Right from '../components/power/Rights.vue'
//引入角色管理组件
import Roler from '../components/power/Roles.vue'
//引入商品分类模块
import Cate from '../components/goods/Cate.vue'
//引入商品分类参数组件
import Params from '../components/goods/Param.vue'
//导入商品列表组件
import GoodList from '../components/goods/List.vue'
//引入商品添加页组件
import GoodAdd from '../components/goods/Add.vue'
//导入订单管理组件
import Order from '../components/order/Order.vue'
//导入报表组件
import Report from '../components/report/Report.vue'


Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/login'
}, {
  path: '/login',
  component: Login
},{
  path: '/home',
  component: Home,
  redirect: '/welcome',
  children: [{
    path: '/welcome',
    component: Welcome
  },{
    path: '/users',
    component: User
  },{
    path: '/rights',
    component: Right
  },{
    path: '/roles',
    component: Roler
  },{
    path: '/categories',
    component: Cate
  },{
    path: '/params',
    component: Params
  },{
    path:'/goods',
    component:GoodList
  },{
    path: '/goods/add',
    component:GoodAdd
  },{
    path: '/orders',
    component:Order
  },{
    path: '/reports',
    component:Report
  }]
}]

const router = new VueRouter({
  routes
})

//设置路由导航守卫
router.beforeEach((to, from, next) => {
  //判断目标路由键是否为登录页 如果是则跳转
  if(to.path == '/login') return next();
  //如果不是 获取sessionStorage中的token
  const token = sessionStorage.getItem('token');
  //如果token不存在，跳转至登录页
  if(!token) return next('/login');
  //如果token存在，则放行
  next();
})

export default router
